package com.sys.web.UI;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.sys.utils.VerifyCodeUtils;

/**
 * @ClassName: VerCode 
 * @Description: TODO(使用VerifyCodeUtils生成注册验证码的类) 
 * @author 詹金浩 
 * @date 2018年3月10日 下午3:07:43
 */
@WebServlet("/verCode")
public class VerCode extends HttpServlet {

	private static final long serialVersionUID = 1L;
	
	//向客户端写入验证码
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		String verCode = VerifyCodeUtils.generateVerifyCode(4);
		//获得一个session对象
		HttpSession session = request.getSession();
		
		//删除之前正确的验证码（如果有的话），并且设置为当前正确的验证码
		session.removeAttribute("rightVerCode");
		session.setAttribute("rightVerCode", verCode);
		
		//生成一个100px * 46px像素的验证码图片
		VerifyCodeUtils.outputImage(100, 46, response.getOutputStream(), verCode);
		
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}